home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / gnu-c / src / gcc-2.7.0-amiga / cp / gc.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  47KB  |  1,557 lines

  1. /* Garbage collection primitives for GNU C++.
  2.    Copyright (C) 1992, 1993, 1995 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22.  
  23. #include "config.h"
  24. #include "tree.h"
  25. #include "cp-tree.h"
  26. #include "flags.h"
  27. #include "output.h"
  28.  
  29. #undef NULL
  30. #define NULL 0
  31.  
  32. extern tree define_function ();
  33. extern tree build_t_desc_overload ();
  34. extern struct obstack *permanent_obstack;
  35.  
  36. /* This is the function decl for the (pseudo-builtin) __gc_protect
  37.    function.  Args are (class *value, int index); Returns value.  */
  38. tree gc_protect_fndecl;
  39.  
  40. /* This is the function decl for the (pseudo-builtin) __gc_unprotect
  41.    function.  Args are (int index); void return.  */
  42. tree gc_unprotect_fndecl;
  43.  
  44. /* This is the function decl for the (pseudo-builtin) __gc_push
  45.    function.  Args are (int length); void return.  */
  46. tree gc_push_fndecl;
  47.  
  48. /* This is the function decl for the (pseudo-builtin) __gc_pop
  49.    function.  Args are void; void return.  */
  50. tree gc_pop_fndecl;
  51.  
  52. /* Special integers that are used to represent bits in gc-safe objects.  */
  53. tree gc_nonobject;
  54. tree gc_visible;
  55. tree gc_white;
  56. tree gc_offwhite;
  57. tree gc_grey;
  58. tree gc_black;
  59.  
  60. /* in c-common.c */
  61. extern tree combine_strings PROTO((tree));
  62.  
  63. /* Predicate that returns non-zero if TYPE needs some kind of
  64.    entry for the GC.  Returns zero otherwise.  */
  65. int
  66. type_needs_gc_entry (type)
  67.      tree type;
  68. {
  69.   tree ttype = type;
  70.  
  71.   if (! flag_gc || type == error_mark_node)
  72.     return 0;
  73.  
  74.   /* Aggregate types need gc entries if any of their members
  75.      need gc entries.  */
  76.   if (IS_AGGR_TYPE (type))
  77.     {
  78.       tree binfos;
  79.       tree fields = TYPE_FIELDS (type);
  80.       int i;
  81.  
  82.       /* We don't care about certain pointers.  Pointers
  83.      to virtual baseclasses are always up front.  We also
  84.      cull out virtual function table pointers because it's
  85.      easy, and it simplifies the logic.*/
  86.       while (fields
  87.          && (DECL_NAME (fields) == NULL_TREE
  88.          || VFIELD_NAME_P (DECL_NAME (fields))
  89.          || VBASE_NAME_P (DECL_NAME (fields))
  90.          || !strcmp (IDENTIFIER_POINTER (DECL_NAME (fields)), "__bits")))
  91.     fields = TREE_CHAIN (fields);
  92.  
  93.       while (fields)
  94.     {
  95.       if (type_needs_gc_entry (TREE_TYPE (fields)))
  96.         return 1;
  97.       fields = TREE_CHAIN (fields);
  98.     }
  99.  
  100.       binfos = TYPE_BINFO_BASETYPES (type);
  101.       if (binfos)
  102.     for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
  103.       if (type_needs_gc_entry (BINFO_TYPE (TREE_VEC_ELT (binfos, i))))
  104.         return 1;
  105.  
  106.       return 0;
  107.     }
  108.  
  109.   while (TREE_CODE (ttype) == ARRAY_TYPE
  110.      && TREE_CODE (TREE_TYPE (ttype)) == ARRAY_TYPE)
  111.     ttype = TREE_TYPE (ttype);
  112.   if ((TREE_CODE (ttype) == POINTER_TYPE
  113.        || TREE_CODE (ttype) == ARRAY_TYPE
  114.        || TREE_CODE (ttype) == REFERENCE_TYPE)
  115.       && IS_AGGR_TYPE (TREE_TYPE (ttype))
  116.       && CLASSTYPE_RTTI (TREE_TYPE (ttype)))
  117.     return 1;
  118.  
  119.   return 0;
  120. }
  121.  
  122. /* Predicate that returns non-zero iff FROM is safe from the GC.
  123.    
  124.    If TO is nonzero, it means we know that FROM is being stored
  125.    in TO, which make make it safe.  */
  126. int
  127. value_safe_from_gc (to, from)
  128.      tree to, from;
  129. {
  130.   /* First, return non-zero for easy cases: parameters,
  131.      static variables.  */
  132.   if (TREE_CODE (from) == PARM_DECL
  133.       || (TREE_CODE (from) == VAR_DECL
  134.       && TREE_STATIC (from)))
  135.     return 1;
  136.  
  137.   /* If something has its address taken, it cannot be
  138.      in the heap, so it doesn't need to be protected.  */
  139.   if (TREE_CODE (from) == ADDR_EXPR || TREE_REFERENCE_EXPR (from))
  140.     return 1;
  141.  
  142.   /* If we are storing into a static variable, then what
  143.      we store will be safe from the gc.  */
  144.   if (to && TREE_CODE (to) == VAR_DECL
  145.       && TREE_STATIC (to))
  146.     return 1;
  147.  
  148.   /* Now recurse on structure of FROM.  */
  149.   switch (TREE_CODE (from))
  150.     {
  151.     case COMPONENT_REF:
  152.       /* These guys are special, and safe.  */
  153.       if (TREE_CODE (TREE_OPERAND (from, 1)) == FIELD_DECL
  154.       && (VFIELD_NAME_P (DECL_NAME (TREE_OPERAND (from, 1)))
  155.           || VBASE_NAME_P (DECL_NAME (TREE_OPERAND (from, 1)))))
  156.     return 1;
  157.       /* fall through...  */
  158.     case NOP_EXPR:
  159.     case CONVERT_EXPR:
  160.     case NON_LVALUE_EXPR:
  161.     case WITH_CLEANUP_EXPR:
  162.     case SAVE_EXPR:
  163.     case PREDECREMENT_EXPR:
  164.     case PREINCREMENT_EXPR:
  165.     case POSTDECREMENT_EXPR:
  166.     case POSTINCREMENT_EXPR:
  167.       if (value_safe_from_gc (to, TREE_OPERAND (from, 0)))
  168.     return 1;
  169.       break;
  170.  
  171.     case VAR_DECL:
  172.     case PARM_DECL:
  173.       /* We can safely pass these things as parameters to functions.  */
  174.       if (to == 0)
  175.     return 1;
  176.  
  177.     case ARRAY_REF:
  178.     case INDIRECT_REF:
  179.     case RESULT_DECL:
  180.     case OFFSET_REF:
  181.     case CALL_EXPR:
  182.     case METHOD_CALL_EXPR:
  183.       break;
  184.  
  185.     case COMPOUND_EXPR:
  186.     case TARGET_EXPR:
  187.       if (value_safe_from_gc (to, TREE_OPERAND (from, 1)))
  188.     return 1;
  189.       break;
  190.  
  191.     case COND_EXPR:
  192.       if (value_safe_from_gc (to, TREE_OPERAND (from, 1))
  193.       && value_safe_from_gc (to, TREE_OPERAND (from, 2)))
  194.     return 1;
  195.       break;
  196.  
  197.     case PLUS_EXPR:
  198.     case MINUS_EXPR:
  199.       if ((type_needs_gc_entry (TREE_TYPE (TREE_OPERAND (from, 0)))
  200.        || value_safe_from_gc (to, TREE_OPERAND (from, 0)))
  201.       && (type_needs_gc_entry (TREE_TYPE (TREE_OPERAND (from, 1))) == 0
  202.           || value_safe_from_gc (to, TREE_OPERAND (from, 1))))
  203.     return 1;
  204.       break;
  205.  
  206.     case RTL_EXPR:
  207.       /* Every time we build an RTL_EXPR in the front-end, we must
  208.      ensure that everything in it is safe from the garbage collector.
  209.      ??? This has only been done for `build_new'.  */
  210.       return 1;
  211.  
  212.     default:
  213.       my_friendly_abort (41);
  214.     }
  215.  
  216.   if (to == 0)
  217.     return 0;
  218.  
  219.   /* FROM wasn't safe.  But other properties of TO might make it safe.  */
  220.   switch (TREE_CODE (to))
  221.     {
  222.     case VAR_DECL:
  223.     case PARM_DECL:
  224.       /* We already culled out static VAR_DECLs above.  */
  225.       return 0;
  226.  
  227.     case COMPONENT_REF:
  228.       /* These guys are special, and safe.  */
  229.       if (TREE_CODE (TREE_OPERAND (to, 1)) == FIELD_DECL
  230.       && (VFIELD_NAME_P (DECL_NAME (TREE_OPERAND (to, 1)))
  231.           || VBASE_NAME_P (DECL_NAME (TREE_OPERAND (to, 1)))))
  232.     return 1;
  233.       /* fall through...  */
  234.  
  235.     case NOP_EXPR:
  236.     case NON_LVALUE_EXPR:
  237.     case WITH_CLEANUP_EXPR:
  238.     case SAVE_EXPR:
  239.     case PREDECREMENT_EXPR:
  240.     case PREINCREMENT_EXPR:
  241.     case POSTDECREMENT_EXPR:
  242.     case POSTINCREMENT_EXPR:
  243.       return value_safe_from_gc (TREE_OPERAND (to, 0), from);
  244.  
  245.     case COMPOUND_EXPR:
  246.     case TARGET_EXPR:
  247.       return value_safe_from_gc (TREE_OPERAND (to, 1), from);
  248.  
  249.     case COND_EXPR:
  250.       return (value_safe_from_gc (TREE_OPERAND (to, 1), from)
  251.           && value_safe_from_gc (TREE_OPERAND (to, 2), from));
  252.  
  253.     case INDIRECT_REF:
  254.     case ARRAY_REF:
  255.       /* This used to be 0, but our current restricted model
  256.      allows this to be 1.  We'll never get arrays this way.  */
  257.       return 1;
  258.  
  259.     default:
  260.       my_friendly_abort (42);
  261.     }
  262.  
  263.   /* Catch-all case is that TO/FROM is not safe.  */
  264.   return 0;
  265. }
  266.  
  267. /* Function to build a static GC entry for DECL.  TYPE is DECL's type.
  268.  
  269.    For objects of type `class *', this is just an entry in the
  270.    static vector __PTR_LIST__.
  271.  
  272.    For objects of type `class[]', this requires building an entry
  273.    in the static vector __ARR_LIST__.
  274.  
  275.    For aggregates, this records all fields of type `class *'
  276.    and `class[]' in the respective lists above.  */
  277. void
  278. build_static_gc_entry (decl, type)
  279.      tree decl;
  280.      tree type;
  281. {
  282.   /* Now, figure out what sort of entry to build.  */
  283.   if (TREE_CODE (type) == POINTER_TYPE
  284.       || TREE_CODE (type) == REFERENCE_TYPE)
  285.     assemble_gc_entry (IDENTIFIER_POINTER (DECL_NAME (decl)));
  286.   else if (TREE_CODE (type) == RECORD_TYPE)
  287.     {
  288.       tree ref = get_temp_name (build_reference_type (type), 1);
  289.       DECL_INITIAL (ref) = build1 (ADDR_EXPR, TREE_TYPE (ref), decl);
  290.       TREE_CONSTANT (DECL_INITIAL (ref)) = 1;
  291.       cp_finish_decl (ref, DECL_INITIAL (ref), NULL_TREE, 0, 0);
  292.     }
  293.   else
  294.     {
  295.       /* Not yet implemented.
  296.      
  297.      Cons up a static variable that holds address and length info
  298.      and add that to ___ARR_LIST__.  */
  299.       my_friendly_abort (43);
  300.     }
  301. }
  302.  
  303. /* Protect FROM from the GC, assuming FROM is going to be
  304.    stored into TO.  We handle three cases for TO here:
  305.  
  306.    case 1: TO is a stack variable.
  307.    case 2: TO is zero (which means it is a parameter).
  308.    case 3: TO is a return value.  */
  309.  
  310. tree
  311. protect_value_from_gc (to, from)
  312.      tree to, from;
  313. {
  314.   if (to == 0)
  315.     {
  316.       tree cleanup;
  317.  
  318.       to = get_temp_regvar (TREE_TYPE (from), from);
  319.  
  320.       /* Convert from integer to list form since we'll use it twice.  */
  321.       DECL_GC_OFFSET (to) = build_tree_list (NULL_TREE, DECL_GC_OFFSET (to));
  322.       cleanup = build_function_call (gc_unprotect_fndecl,
  323.                      DECL_GC_OFFSET (to));
  324.  
  325.       if (! expand_decl_cleanup (to, cleanup))
  326.     {
  327.       compiler_error ("cannot unprotect parameter in this scope");
  328.       return error_mark_node;
  329.     }
  330.     }
  331.  
  332.   /* Should never need to protect a value that's headed for static storage.  */
  333.   if (TREE_STATIC (to))
  334.     my_friendly_abort (44);
  335.  
  336.   switch (TREE_CODE (to))
  337.     {
  338.     case COMPONENT_REF:
  339.     case INDIRECT_REF:
  340.       return protect_value_from_gc (TREE_OPERAND (to, 0), from);
  341.  
  342.     case VAR_DECL:
  343.     case PARM_DECL:
  344.       {
  345.     tree rval;
  346.     if (DECL_GC_OFFSET (to) == NULL_TREE)
  347.       {
  348.         /* Because of a cast or a conversion, we might stick
  349.            a value into a variable that would not normally
  350.            have a GC entry.  */
  351.         DECL_GC_OFFSET (to) = size_int (++current_function_obstack_index);
  352.       }
  353.  
  354.     if (TREE_CODE (DECL_GC_OFFSET (to)) != TREE_LIST)
  355.       {
  356.         DECL_GC_OFFSET (to)
  357.           = build_tree_list (NULL_TREE, DECL_GC_OFFSET (to));
  358.       }
  359.  
  360.     current_function_obstack_usage = 1;
  361.     rval = build_function_call (gc_protect_fndecl,
  362.                     tree_cons (NULL_TREE, from,
  363.                            DECL_GC_OFFSET (to)));
  364.     TREE_TYPE (rval) = TREE_TYPE (from);
  365.     return rval;
  366.       }
  367.     }
  368.  
  369.   /* If we fall through the switch, assume we lost.  */
  370.   my_friendly_abort (45);
  371.   /* NOTREACHED */
  372.   return NULL_TREE;
  373. }
  374.  
  375. /* Given the expression EXP of type `class *', return the head
  376.    of the object pointed to by EXP.  */
  377. tree
  378. build_headof (exp)
  379.      tree exp;
  380. {
  381.   tree type = TREE_TYPE (exp);
  382.   tree vptr, offset;
  383.  
  384.   if (TREE_CODE (type) != POINTER_TYPE)
  385.     {
  386.       error ("`headof' applied to non-pointer type");
  387.       return error_mark_node;
  388.     }
  389.   type = TREE_TYPE (type);
  390.  
  391.   if (!TYPE_VIRTUAL_P (type) || CLASSTYPE_VFIELD (type) == NULL_TREE)
  392.     return (exp);
  393.  
  394.   if (flag_vtable_thunks)
  395.     abort();
  396.  
  397.   vptr = fold (size_binop (PLUS_EXPR, 
  398.        size_binop (FLOOR_DIV_EXPR, 
  399.            DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (type)),
  400.          size_int (BITS_PER_UNIT)),
  401.         exp)); 
  402.   vptr = build1 (INDIRECT_REF, TYPE_POINTER_TO (vtable_entry_type), vptr);
  403.   offset = build_component_ref (build_array_ref (vptr, integer_zero_node),
  404.                 delta_identifier,
  405.                 NULL_TREE, 0);
  406.   return build (PLUS_EXPR, class_star_type_node, exp,
  407.         convert (ptrdiff_type_node, offset));
  408. }
  409.  
  410. /* Given the expression EXP of type `class *', return the
  411.    type descriptor for the object pointed to by EXP.  */
  412. tree
  413. build_classof (exp)
  414.      tree exp;
  415. {
  416.   tree type = TREE_TYPE (exp);
  417.   tree vptr;
  418.   tree t_desc_entry;
  419.  
  420.   if (TREE_CODE (type) != POINTER_TYPE)
  421.     {
  422.       error ("`classof' applied to non-pointer type");
  423.       return error_mark_node;
  424.     }
  425.  
  426.   vptr = build1 (INDIRECT_REF, TYPE_POINTER_TO (vtable_entry_type), exp);
  427.   t_desc_entry = build_component_ref (build_array_ref (vptr, integer_one_node),
  428.                       get_identifier (VTABLE_PFN_NAME),
  429.                       NULL_TREE, 0);
  430.   TREE_TYPE (t_desc_entry) = TYPE_POINTER_TO (__t_desc_type_node);
  431.   return t_desc_entry;
  432. }
  433.  
  434. /* Return the type_info node associated with the expression EXP.  If EXP is
  435.    a reference to a polymorphic class, return the dynamic type; otherwise
  436.    return the static type of the expression.  */
  437. tree
  438. build_typeid (exp)
  439.      tree exp;
  440. {
  441.   tree type;
  442.  
  443.   if (!flag_rtti)
  444.     cp_error ("cannot take typeid of object when -frtti is not specified");
  445.  
  446.   if (exp == error_mark_node)
  447.     return error_mark_node;
  448.  
  449.   type = TREE_TYPE (exp);
  450. #if 0
  451.   /* Fergus Henderson <fjh@munta.cs.mu.OZ.AU> says we decided at the Waterloo
  452.      meeting to strip the top cv-qualifiers. */
  453.   if (TYPE_VOLATILE (exp) || TYPE_READONLY (exp))
  454.     type = build_type_variant (type, TYPE_READONLY (exp), TYPE_VOLATILE (exp));
  455. #else
  456.   /* Strip top-level cv-qualifiers.  */
  457.   type = TYPE_MAIN_VARIANT (type);
  458. #endif
  459.  
  460.   /* if b is an instance of B, typeid(b) == typeid(B).  Do this before
  461.      reference trickiness.  */
  462.   if (TREE_CODE (exp) == VAR_DECL && TREE_CODE (type) == RECORD_TYPE)
  463.     return get_typeid (type);
  464.  
  465.   /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
  466.   if (TREE_CODE (type) == RECORD_TYPE)
  467.     type = build_reference_type (type);
  468.  
  469.   /* If exp is a reference to polymorphic type, get the real type_info.  */
  470.   if (TREE_CODE (type) == REFERENCE_TYPE && TYPE_VIRTUAL_P (TREE_TYPE (type)))
  471.     {
  472.       /* build reference to type_info from vtable.  */
  473.       tree t;
  474.       t = build_vfn_ref ((tree *) NULL_TREE, exp, build_int_2 (0, 0));  
  475.       TREE_TYPE (t) = build_pointer_type (__class_desc_type_node);
  476.       t = build_indirect_ref (t, NULL);
  477.       return t;
  478.     }
  479.  
  480.   /* otherwise return the type_info for the static type of the expr.  */
  481.   return get_typeid (type);
  482. }
  483.  
  484. /* Return the type_info object for TYPE, creating it if necessary.  */
  485. tree
  486. get_typeid (type)
  487.      tree type;
  488. {
  489.   tree t, td;
  490.  
  491.   if (type == error_mark_node)
  492.     return error_mark_node;
  493.   
  494.   /* Is it useful (and/or correct) to have different typeids for `T &'
  495.      and `T'?  */
  496.   if (TREE_CODE (type) == REFERENCE_TYPE)
  497.     type = TREE_TYPE (type);
  498.  
  499.   td = build_t_desc (type, 1);
  500.   if (td == error_mark_node)
  501.     return error_mark_node;
  502.  
  503.   t = TREE_OPERAND (td, 0);
  504.   return t;
  505. }
  506.  
  507. /* Get a bad_cast node for the program to throw...
  508.  
  509.    See libstdc++::exception{,.cc} for __bad_cast_object */
  510. tree
  511. get_bad_cast_node ()
  512. {
  513.   static tree t;
  514.   if (t == NULL_TREE
  515.       && (t = lookup_name (get_identifier ("__bad_cast_object"), 0))
  516.          == NULL_TREE)
  517.     {
  518.       error ("you must #include <typeinfo>");
  519.       return error_mark_node;
  520.     }
  521.   return t;
  522. }
  523.  
  524. /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
  525.    paper.  */
  526. tree
  527. build_dynamic_cast (type, expr)
  528.      tree type, expr;
  529. {
  530.   enum tree_code tc = TREE_CODE (type);
  531.   tree exprtype = TREE_TYPE (expr);
  532.   enum tree_code ec = TREE_CODE (exprtype);
  533.   tree retval;
  534.  
  535.   if (type == error_mark_node || expr == error_mark_node)
  536.     return error_mark_node;
  537.   
  538.   switch (tc)
  539.     {
  540.     case POINTER_TYPE:
  541.       if (ec == REFERENCE_TYPE)
  542.     {
  543.       expr = convert_from_reference (expr);
  544.       exprtype = TREE_TYPE (expr);
  545.       ec = TREE_CODE (exprtype);
  546.     }
  547.       if (ec != POINTER_TYPE)
  548.     goto fail;
  549.       if (TREE_CODE (TREE_TYPE (exprtype)) != RECORD_TYPE)
  550.     goto fail;
  551.       if (TYPE_SIZE (TREE_TYPE (exprtype)) == 0)
  552.     goto fail;
  553.       if (TREE_TYPE (type) == void_type_node)
  554.     break;
  555.       /* else fall through */
  556.     case REFERENCE_TYPE:
  557.       if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
  558.       && TYPE_SIZE (TREE_TYPE (type)) != NULL_TREE)
  559.     break;
  560.       /* else fall through */
  561.     default:
  562.       goto fail;
  563.     }
  564.  
  565.   /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
  566.   if (ec == RECORD_TYPE)
  567.     {
  568.       exprtype = build_reference_type (exprtype);
  569.       ec = REFERENCE_TYPE;
  570.     }
  571.  
  572.   if (tc == REFERENCE_TYPE)
  573.     {
  574.       if (ec != REFERENCE_TYPE)
  575.     goto fail;
  576.       if (TREE_CODE (TREE_TYPE (exprtype)) != RECORD_TYPE)
  577.     goto fail;
  578.       if (TYPE_SIZE (TREE_TYPE (exprtype)) == 0)
  579.     goto fail;
  580.     }
  581.  
  582.   /* If *type is an unambiguous accessible base class of *exprtype,
  583.      convert statically.  */
  584.   {
  585.     int distance;
  586.     tree path;
  587.  
  588.     distance = get_base_distance (TREE_TYPE (type), TREE_TYPE (exprtype), 1,
  589.                   &path);
  590.     if (distance >= 0)
  591.       return build_vbase_path (PLUS_EXPR, type, expr, path, 0);
  592.   }
  593.  
  594.   /* Otherwise *exprtype must be a polymorphic class (have a vtbl).  */
  595.   if (TYPE_VIRTUAL_P (TREE_TYPE (exprtype)))
  596.     {
  597.       /* if TYPE is `void *', return pointer to complete object.  */
  598.       if (tc == POINTER_TYPE && TREE_TYPE (type) == void_type_node)
  599.     {
  600.       /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b.  */
  601.       if (TREE_CODE (expr) == ADDR_EXPR
  602.           && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
  603.           && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
  604.         return build1 (NOP_EXPR, type, expr);
  605.  
  606.       return build_headof (expr);
  607.     }
  608.       else
  609.     {
  610.       tree retval;
  611.           tree result, td1, td2, elems, tmp1, expr1;
  612.  
  613.        /* If we got here, we can't convert statically.  Therefore,
  614.          dynamic_cast<D&>(b) (b an object) cannot succeed.  */
  615.       if (ec == REFERENCE_TYPE)
  616.         {
  617.           if (TREE_CODE (expr) == VAR_DECL
  618.           && TREE_CODE (TREE_TYPE (expr)) == RECORD_TYPE)
  619.         {
  620.           cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
  621.                   expr, type);
  622.           return build_throw (get_bad_cast_node ());
  623.         }
  624.         }
  625.       /* Ditto for dynamic_cast<D*>(&b).  */
  626.       else if (TREE_CODE (expr) == ADDR_EXPR)
  627.         {
  628.           tree op = TREE_OPERAND (expr, 0);
  629.           if (TREE_CODE (op) == VAR_DECL
  630.           && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
  631.         {
  632.           cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
  633.                   expr, type);
  634.           retval = build_int_2 (0, 0); 
  635.           TREE_TYPE (retval) = type; 
  636.           return retval;
  637.         }
  638.         }
  639.  
  640.       expr1 = expr;
  641.       if (tc == REFERENCE_TYPE)
  642.         expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
  643.  
  644.       /* Build run-time conversion.  */
  645.       expr1 = build_headof (expr1);
  646.  
  647.       if (ec == POINTER_TYPE)
  648.         td1 = build_typeid (build_indirect_ref (expr, NULL_PTR));
  649.       else
  650.         td1 = build_typeid (expr);
  651.       
  652.       if (tc == POINTER_TYPE)
  653.         td2 = get_typeid (TREE_TYPE (type));
  654.       else
  655.         td2 = get_typeid (type);
  656.  
  657.           elems = tree_cons (NULL_TREE, td2,
  658.             tree_cons (NULL_TREE, build_int_2 (1, 0),
  659.           tree_cons (NULL_TREE, expr1, NULL_TREE)));
  660.           result = build_method_call (td1,
  661.             get_identifier ("__rtti_match"), elems, NULL_TREE, LOOKUP_NORMAL);
  662.  
  663.       if (tc == REFERENCE_TYPE)
  664.         {
  665.           expr1 = build_throw (get_bad_cast_node ());
  666.           expr1 = build_compound_expr (tree_cons (NULL_TREE, expr1,
  667.                               build_tree_list (NULL_TREE, convert (type, integer_zero_node))));
  668.           TREE_TYPE (expr1) = type;
  669.           return build (COND_EXPR, type, result, result, expr1);
  670.         }
  671.  
  672.       /* Now back to the type we want from a void*. */
  673.       result = convert (type, result);
  674.           return result;
  675.     }
  676.     }
  677.  
  678.  fail:
  679.   cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T'",
  680.         expr, exprtype, type);
  681.   return error_mark_node;
  682. }
  683.  
  684. /* Build and initialize various sorts of descriptors.  Every descriptor
  685.    node has a name associated with it (the name created by mangling).
  686.    For this reason, we use the identifier as our access to the __*_desc
  687.    nodes, instead of sticking them directly in the types.  Otherwise we
  688.    would burden all built-in types (and pointer types) with slots that
  689.    we don't necessarily want to use.
  690.  
  691.    For each descriptor we build, we build a variable that contains
  692.    the descriptor's information.  When we need this info at runtime,
  693.    all we need is access to these variables.
  694.  
  695.    Note: these constructors always return the address of the descriptor
  696.    info, since that is simplest for their mutual interaction.  */
  697.  
  698. static tree
  699. build_generic_desc (tdecl, type, elems)
  700.      tree tdecl;
  701.      tree type;
  702.      tree elems;
  703. {
  704.   tree init = elems;
  705.   int toplev = global_bindings_p ();
  706.  
  707.   TREE_CONSTANT (init) = 1;
  708.   TREE_STATIC (init) = 1;
  709.   TREE_READONLY (init) = 1;
  710.  
  711.   TREE_TYPE (tdecl) = type;
  712.   DECL_INITIAL (tdecl) = init;
  713.   TREE_STATIC (tdecl) = 1;
  714.   DECL_SIZE (tdecl) = NULL_TREE;
  715.   layout_decl (tdecl, 0);
  716.   if (! toplev)
  717.     push_to_top_level ();
  718.   cp_finish_decl (tdecl, init, NULL_TREE, 0, 0);
  719.   if (! toplev)
  720.     pop_from_top_level ();
  721.  
  722.   if (! TREE_USED (tdecl))
  723.     {
  724.       assemble_external (tdecl);
  725.       TREE_USED (tdecl) = 1;
  726.     }
  727.  
  728.   return IDENTIFIER_AS_DESC (DECL_NAME (tdecl));
  729. }
  730.  
  731. /* Build an initializer for a __bltn_desc node.  */
  732. static tree
  733. build_bltn_desc (tdecl, type)
  734.      tree tdecl;
  735.      tree type;
  736. {
  737.   tree elems, t;
  738.  
  739.   if (type == boolean_type_node)
  740.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_BOOL"),
  741.               0, 0);
  742.   else if (type == char_type_node)
  743.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_CHAR"),
  744.               0, 0);
  745.   else if (type == short_integer_type_node)
  746.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_SHORT"),
  747.               0, 0);
  748.   else if (type == integer_type_node)
  749.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_INT"),
  750.               0, 0);
  751.   else if (type == long_integer_type_node)
  752.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_LONG"),
  753.               0, 0);
  754.   else if (type == long_long_integer_type_node)
  755.     t = lookup_field (__bltn_desc_type_node, 
  756.               get_identifier("_RTTI_BI_LONGLONG"), 0, 0);
  757.   else if (type == float_type_node)
  758.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_FLOAT"),
  759.               0, 0);
  760.   else if (type == double_type_node)
  761.     t = lookup_field (__bltn_desc_type_node, 
  762.               get_identifier("_RTTI_BI_DOUBLE"), 0, 0);
  763.   else if (type == long_double_type_node)
  764.     t = lookup_field (__bltn_desc_type_node, 
  765.               get_identifier("_RTTI_BI_LDOUBLE"), 0, 0);
  766.   else if (type == unsigned_char_type_node)
  767.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_UCHAR"),
  768.               0, 0);
  769.   else if (type == short_unsigned_type_node)
  770.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_USHORT"),
  771.               0, 0);
  772.   else if (type == unsigned_type_node)
  773.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_UINT"),
  774.               0, 0);
  775.   else if (type == long_unsigned_type_node)
  776.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_ULONG"),
  777.               0, 0);
  778.   else if (type == long_long_unsigned_type_node)
  779.     t = lookup_field (__bltn_desc_type_node, 
  780.               get_identifier("_RTTI_BI_ULONGLONG"), 0, 0);
  781.   else if (type == signed_char_type_node)
  782.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_SCHAR"),
  783.               0, 0);
  784.   else if (type == wchar_type_node)
  785.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_WCHAR"),
  786.               0, 0);
  787.   else if (type == void_type_node)
  788.     t = lookup_field (__bltn_desc_type_node, get_identifier("_RTTI_BI_VOID"),
  789.               0, 0);
  790.   else
  791.     {
  792.       cp_compiler_error ("type `%T' not handled as a built-in type");
  793.     }
  794.  
  795.   elems = tree_cons (NULL_TREE, t, NULL_TREE);
  796.   return build_generic_desc (tdecl, __bltn_desc_type_node, elems);
  797. }
  798.  
  799. /* Build an initializer for a __user_desc node.  */
  800. static tree
  801. build_user_desc (tdecl)
  802.      tree tdecl;
  803. {
  804.   tree elems, name_string, t;
  805.   tree tname = DECL_NAME (tdecl);
  806.  
  807.   name_string = combine_strings (build_string 
  808.     (IDENTIFIER_LENGTH (tname)+1, IDENTIFIER_POINTER (tname)));
  809.   elems = name_string;
  810.   return build_generic_desc (tdecl, __user_desc_type_node, elems);
  811. }
  812.  
  813. /* Build an initializer for a __class_type_info node. */
  814. static tree
  815. build_class_desc (tdecl, type)
  816.      tree tdecl;
  817.      tree type;
  818. {
  819.   tree tname = DECL_NAME (tdecl);
  820.   tree name_string;
  821.  
  822.   int i = CLASSTYPE_N_BASECLASSES (type);
  823.   int n_base = i;
  824.   int base_cnt = 0;
  825.   tree binfos = TYPE_BINFO_BASETYPES (type);
  826.   tree vb = CLASSTYPE_VBASECLASSES (type);
  827.   tree base, elems, access, offset, isvir;
  828.   tree base_list, off_list, acc_list, isvir_list;
  829.   tree t;
  830.   static tree acc_pub = NULL_TREE;
  831.   static tree acc_pro = NULL_TREE;
  832.   static tree acc_pri = NULL_TREE;
  833.  
  834.   if (acc_pub == NULL_TREE) 
  835.     {
  836.       acc_pub = lookup_field (__class_desc_type_node, 
  837.                                 get_identifier("_RTTI_ACCESS_PUBLIC"), 0, 0);
  838.       acc_pro = lookup_field (__class_desc_type_node,
  839.                                 get_identifier("_RTTI_ACCESS_PROTECTED"), 0, 0);
  840.       acc_pri = lookup_field (__class_desc_type_node,
  841.                                 get_identifier("_RTTI_ACCESS_PRIVATE"), 0, 0);
  842.     }
  843.  
  844.   base_list = build_tree_list (NULL_TREE, integer_zero_node);
  845.   off_list = build_tree_list (NULL_TREE, integer_zero_node);
  846.   acc_list = build_tree_list (NULL_TREE, integer_zero_node);
  847.   isvir_list = build_tree_list (NULL_TREE, integer_zero_node);
  848.   while (--i >= 0)
  849.     {
  850.       tree binfo = TREE_VEC_ELT (binfos, i);
  851.  
  852.       base = build_t_desc (BINFO_TYPE (binfo), 1);
  853.       if (TREE_VIA_VIRTUAL (binfo))
  854.     {
  855.       tree t = BINFO_TYPE (binfo);
  856.       char *name;
  857.       tree field;
  858.       int off;
  859.  
  860.       name = (char *) alloca (TYPE_NAME_LENGTH (t)+sizeof (VBASE_NAME)+1);
  861.       sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (t));
  862.       field = lookup_field (type, get_identifier (name), 0, 0);
  863.       offset = size_binop (FLOOR_DIV_EXPR, 
  864.         DECL_FIELD_BITPOS (field), size_int (BITS_PER_UNIT));
  865.     }
  866.       else
  867.     offset = BINFO_OFFSET (binfo);
  868.  
  869.       if (TREE_VIA_PUBLIC (binfo))
  870.         access = acc_pub;
  871.       else if (TREE_VIA_PROTECTED (binfo))
  872.     access = acc_pro;
  873.       else
  874.     access = acc_pri;
  875.       if (TREE_VIA_VIRTUAL (binfo))
  876.     isvir = build_int_2 (1, 0);
  877.       else
  878.     isvir = build_int_2 (0, 0);
  879.  
  880.       base_list = tree_cons (NULL_TREE, base, base_list);
  881.       isvir_list = tree_cons (NULL_TREE, isvir, isvir_list);
  882.       acc_list = tree_cons (NULL_TREE, access, acc_list);
  883.       off_list = tree_cons (NULL_TREE, offset, off_list);
  884.       base_cnt++;
  885.     }
  886. #if 0
  887.   i = n_base;
  888.   while (vb)
  889.     {
  890.       tree b;
  891.       access = acc_pub;
  892.       while (--i >= 0)
  893.     {
  894.       b = TREE_VEC_ELT (binfos, i);
  895.       if (BINFO_TYPE (vb) == BINFO_TYPE (b) && TREE_VIA_VIRTUAL (b))
  896.         {
  897.           if (TREE_VIA_PUBLIC (b))
  898.         access = acc_pub;
  899.           else if (TREE_VIA_PROTECTED (b))
  900.         access = acc_pro;
  901.           else
  902.         access = acc_pri;
  903.           break;
  904.         }
  905.     }
  906.       base = build_t_desc (BINFO_TYPE (vb), 1);
  907.       offset = BINFO_OFFSET (vb);
  908.       isvir = build_int_2 (1, 0);
  909.  
  910.       base_list = tree_cons (NULL_TREE, base, base_list);
  911.       isvir_list = tree_cons (NULL_TREE, isvir, isvir_list);
  912.       acc_list = tree_cons (NULL_TREE, access, acc_list);
  913.       off_list = tree_cons (NULL_TREE, offset, off_list);
  914.  
  915.       base_cnt++;
  916.       vb = TREE_CHAIN (vb);
  917.     }
  918. #endif
  919.   base_list = finish_table (NULL_TREE, TYPE_POINTER_TO (__t_desc_type_node), 
  920.                 base_list, 0);
  921.   off_list = finish_table (NULL_TREE, integer_type_node, 
  922.                 off_list, 0);
  923.   isvir_list = finish_table (NULL_TREE, integer_type_node, 
  924.                 isvir_list, 0);
  925.   acc_list = finish_table (NULL_TREE, __access_mode_type_node, 
  926.                 acc_list, 0);
  927.  
  928.  
  929.   name_string = combine_strings (build_string (IDENTIFIER_LENGTH (tname)+1, IDENTIFIER_POINTER (tname)));
  930.  
  931.   elems = tree_cons (NULL_TREE, name_string,
  932.         tree_cons (NULL_TREE, default_conversion (base_list),
  933.           tree_cons (NULL_TREE, default_conversion (off_list),
  934.         tree_cons (NULL_TREE, default_conversion (isvir_list),
  935.           tree_cons (NULL_TREE, default_conversion (acc_list),
  936.                   tree_cons (NULL_TREE, build_int_2 (base_cnt, 0), NULL_TREE))))));
  937.  
  938.   return build_generic_desc (tdecl, __class_desc_type_node, elems);
  939. }
  940.  
  941. /* Build an initializer for a __pointer_type_info node.  */
  942. static tree
  943. build_ptr_desc (tdecl, type)
  944.      tree tdecl;
  945.      tree type;
  946. {
  947.   tree t, elems;
  948.  
  949.   t = TREE_TYPE (type);
  950.   t = build_t_desc (t, 1);
  951.   t = build_indirect_ref (t, NULL);
  952.   elems = tree_cons (NULL_TREE, t, NULL_TREE);
  953.   return build_generic_desc (tdecl, __ptr_desc_type_node,  elems);
  954. }
  955.  
  956. /* Build an initializer for a __attr_type_info node.  */
  957. static tree
  958. build_attr_desc (tdecl, type)
  959.      tree tdecl;
  960.      tree type;
  961. {
  962.   tree elems, t, attrval;
  963.  
  964.   if (TYPE_READONLY (type))
  965.     {
  966.       if (TYPE_VOLATILE (type))
  967.     attrval = lookup_field (__attr_desc_type_node, 
  968.                 get_identifier("_RTTI_ATTR_CONSTVOL"), 0, 0);
  969.       else
  970.     attrval = lookup_field (__attr_desc_type_node, 
  971.                 get_identifier("_RTTI_ATTR_CONST"), 0, 0);
  972.     }
  973.   else
  974.     {
  975.       if (TYPE_VOLATILE (type))
  976.     attrval = lookup_field (__attr_desc_type_node, 
  977.                 get_identifier("_RTTI_ATTR_VOLATILE"), 0, 0);
  978.     }
  979.   t = build_t_desc (TYPE_MAIN_VARIANT (type), 1);
  980.   t = build_indirect_ref (t , NULL);
  981.   elems = tree_cons (NULL_TREE, attrval, tree_cons (NULL_TREE, t, NULL_TREE));
  982.   return build_generic_desc (tdecl, __attr_desc_type_node,  elems);
  983. }
  984.  
  985. /* Build an initializer for a __func_type_info node.  */
  986. static tree
  987. build_func_desc (tdecl)
  988.      tree tdecl;
  989. {
  990.   tree elems, name_string;
  991.   tree tname = DECL_NAME (tdecl);
  992.  
  993.   name_string = combine_strings (build_string 
  994.     (IDENTIFIER_LENGTH (tname)+1, IDENTIFIER_POINTER (tname)));
  995.   elems = name_string; 
  996.   return build_generic_desc (tdecl, __func_desc_type_node,  elems);
  997. }
  998.  
  999. /* Build an initializer for a __ptmf_type_info node.  */
  1000. static tree
  1001. build_ptmf_desc (tdecl, type)
  1002.      tree tdecl;
  1003.      tree type;
  1004.   tree elems, name_string;
  1005.   tree tname = DECL_NAME (tdecl);
  1006.  
  1007.   name_string = combine_strings (build_string 
  1008.     (IDENTIFIER_LENGTH (tname)+1, IDENTIFIER_POINTER (tname)));
  1009.   elems = name_string; 
  1010.   return build_generic_desc (tdecl, __ptmf_desc_type_node,  elems);
  1011. }
  1012.  
  1013. /* Build an initializer for a __ptmd_type_info node.  */
  1014. static tree
  1015. build_ptmd_desc (tdecl, type)
  1016.      tree tdecl;
  1017.      tree type;
  1018. {
  1019.   tree tc, t, elems;
  1020.   tc = build_t_desc (TYPE_OFFSET_BASETYPE (type), 1);
  1021.   tc = build_indirect_ref (tc , NULL);
  1022.   t = build_t_desc (TREE_TYPE (type), 1);
  1023.   t = build_indirect_ref (t , NULL);
  1024.   elems = tree_cons (NULL_TREE, tc,
  1025.         tree_cons (NULL_TREE, t, NULL_TREE));
  1026.   return build_generic_desc (tdecl, __ptmd_desc_type_node,  elems);
  1027. }
  1028.  
  1029. struct uninst_st {
  1030.   tree type;
  1031.   struct uninst_st *next;
  1032. };
  1033. typedef struct uninst_st uninst_node;
  1034. static uninst_node * uninst_desc = (uninst_node *)NULL;
  1035.  
  1036. static void
  1037. add_uninstantiated_desc (type)
  1038.      tree type;
  1039. {
  1040.   uninst_node *t;
  1041.  
  1042.   t = (uninst_node *) xmalloc (sizeof (struct uninst_st));
  1043.   t->type = type;
  1044.   t->next = uninst_desc;
  1045.   uninst_desc = t;
  1046. }
  1047.  
  1048. /* We may choose to link the emitting of certain high use TDs for certain
  1049.    objects, we do that here.  Return the type to link against if such a
  1050.    link exists, otherwise just return TYPE.  */
  1051.  
  1052. tree
  1053. get_def_to_follow (type)
  1054.      tree type;
  1055. {
  1056. #if 0
  1057.   /* For now we don't lay out T&, T* TDs with the main TD for the object.  */
  1058.   /* Let T* and T& be written only when T is written (if T is an aggr).
  1059.      We do this for const, but not for volatile, since volatile
  1060.      is rare and const is not.  */
  1061.   if (!TYPE_VOLATILE (taggr)
  1062.       && (TREE_CODE (taggr) == POINTER_TYPE
  1063.       || TREE_CODE (taggr) == REFERENCE_TYPE)
  1064.       && IS_AGGR_TYPE (TREE_TYPE (taggr)))
  1065.     taggr = TREE_TYPE (taggr);
  1066. #endif
  1067.   return type;
  1068. }
  1069.  
  1070. /* build a general type_info node. */
  1071. tree
  1072. build_t_desc (type, definition)
  1073.      tree type;
  1074.      int definition;
  1075. {
  1076.   tree tdecl;
  1077.   tree tname, name_string;
  1078.   tree elems;
  1079.   tree t, tt, taggr;
  1080.  
  1081.   if (__ptmd_desc_type_node == NULL_TREE)
  1082.     {
  1083.       init_type_desc();
  1084.       if (__ptmd_desc_type_node)
  1085.     {
  1086.           for ( ; uninst_desc; uninst_desc = uninst_desc->next )
  1087.         build_t_desc (uninst_desc->type, 1);
  1088.     }
  1089.     }
  1090.   if (__t_desc_type_node == NULL_TREE)
  1091.     {
  1092.       static int warned = 0;
  1093.       if (! warned)
  1094.     {
  1095.       cp_error ("failed to build type descriptor node of '%T', maybe typeinfo.h not included", type);
  1096.     }
  1097.       warned = 1;
  1098.       return error_mark_node;
  1099.     }
  1100.   if (__ptmd_desc_type_node == NULL_TREE)
  1101.     {
  1102.       add_uninstantiated_desc (type);
  1103.       definition = 0;
  1104.     }
  1105.  
  1106.   push_obstacks (&permanent_obstack, &permanent_obstack);
  1107.   tname = build_t_desc_overload (type);
  1108.  
  1109.   if (!IDENTIFIER_AS_DESC (tname))
  1110.     {
  1111.       tdecl = build_decl (VAR_DECL, tname, __t_desc_type_node);
  1112.       DECL_EXTERNAL (tdecl) = 1;
  1113.       TREE_PUBLIC (tdecl) = 1;
  1114.       tdecl = pushdecl_top_level (tdecl);
  1115.       SET_IDENTIFIER_AS_DESC (tname, build_unary_op (ADDR_EXPR, tdecl, 0));
  1116.       if (!definition)
  1117.     cp_finish_decl (tdecl, NULL_TREE, NULL_TREE, 0, 0);
  1118.     }
  1119.   else
  1120.     tdecl = TREE_OPERAND (IDENTIFIER_AS_DESC (tname), 0);
  1121.  
  1122.   /* If it's not a definition, don't do anything more.  */
  1123.   if (!definition)
  1124.     return IDENTIFIER_AS_DESC (tname);
  1125.  
  1126.   /* If it has already been written, don't to anything more.  */
  1127.   /* Should this be on tdecl? */
  1128.   if (TREE_ASM_WRITTEN (IDENTIFIER_AS_DESC (tname)))
  1129.     return IDENTIFIER_AS_DESC (tname);
  1130.  
  1131.   /* If we previously defined it, return the defined result.  */
  1132.   if (DECL_INITIAL (tdecl))
  1133.     return IDENTIFIER_AS_DESC (tname);
  1134.     
  1135.   taggr = get_def_to_follow (type);
  1136.  
  1137.   /* If we know that we don't need to write out this type's
  1138.      vtable, then don't write out it's type_info.  Somebody
  1139.      else will take care of that.  */
  1140.   if (IS_AGGR_TYPE (taggr) && CLASSTYPE_VFIELD (taggr))
  1141.     {
  1142.       /* Let's play follow the vtable. */
  1143.       TREE_PUBLIC (tdecl) = CLASSTYPE_INTERFACE_KNOWN (taggr);
  1144.       DECL_EXTERNAL (tdecl) = CLASSTYPE_INTERFACE_ONLY (taggr);
  1145.     }
  1146.   else
  1147.     {
  1148.       DECL_EXTERNAL (tdecl) = 0;
  1149.       TREE_PUBLIC (tdecl) = (definition > 1);
  1150.     }
  1151.  
  1152.   if (DECL_EXTERNAL (tdecl))
  1153.     return IDENTIFIER_AS_DESC (tname);
  1154.  
  1155.   /* Show that we are defining the t_desc for this type.  */
  1156.   DECL_INITIAL (tdecl) = error_mark_node;
  1157.   t = DECL_CONTEXT (tdecl);
  1158.   if ( t && TREE_CODE_CLASS (TREE_CODE (t)) == 't') 
  1159.     pushclass (t, 2);
  1160.  
  1161.   if (TYPE_VOLATILE (type) || TYPE_READONLY (type))
  1162.     t = build_attr_desc (tdecl, type);
  1163.   else if (TREE_CODE (type) == ARRAY_TYPE)
  1164.     t = build_ptr_desc (tdecl, type);
  1165.   else if (TREE_CODE (type) == POINTER_TYPE)
  1166.     {
  1167.       if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE)
  1168.     {
  1169.       type = TREE_TYPE (type);
  1170.       t = build_ptmd_desc (tdecl, type);
  1171.     }
  1172.       else
  1173.     {
  1174.       t = build_ptr_desc (tdecl, type);
  1175.     }
  1176.     }
  1177.   else if (TYPE_BUILT_IN (type))
  1178.     t = build_bltn_desc (tdecl, type);
  1179.   else if (IS_AGGR_TYPE (type))
  1180.     {
  1181.       if (TYPE_PTRMEMFUNC_P (type))
  1182.     {
  1183.       t = build_ptmf_desc (tdecl, type);
  1184.     }
  1185.       else
  1186.     {
  1187.       t = build_class_desc (tdecl, type);
  1188.     }
  1189.     }
  1190.   else if (TREE_CODE (type) == FUNCTION_TYPE)
  1191.     t = build_func_desc (tdecl);
  1192.   else 
  1193.     t = build_user_desc (tdecl);
  1194.  
  1195.   pop_obstacks ();
  1196.   return t;
  1197. }
  1198.  
  1199. #if 0
  1200. /* This is the old dossier type descriptor generation code, it's much
  1201.    more extended than rtti. It's reserved for later use. */
  1202. /* Build an initializer for a __t_desc node.  So that we can take advantage
  1203.    of recursion, we accept NULL for TYPE.
  1204.    DEFINITION is greater than zero iff we must define the type descriptor
  1205.    (as opposed to merely referencing it).  1 means treat according to
  1206.    #pragma interface/#pragma implementation rules.  2 means define as
  1207.    global and public, no matter what.  */
  1208. tree
  1209. build_t_desc (type, definition)
  1210.      tree type;
  1211.      int definition;
  1212. {
  1213.   tree tdecl;
  1214.   tree tname, name_string;
  1215.   tree elems, fields;
  1216.   tree parents, vbases, offsets, ivars, methods, target_type;
  1217.   int method_count = 0, field_count = 0;
  1218.  
  1219.   if (type == NULL_TREE)
  1220.     return NULL_TREE;
  1221.  
  1222.   tname = build_t_desc_overload (type);
  1223.   if (IDENTIFIER_AS_DESC (tname)
  1224.       && (!definition || TREE_ASM_WRITTEN (IDENTIFIER_AS_DESC (tname))))
  1225.     return IDENTIFIER_AS_DESC (tname);
  1226.  
  1227.   tdecl = lookup_name (tname, 0);
  1228.   if (tdecl == NULL_TREE)
  1229.     {
  1230.       tdecl = build_decl (VAR_DECL, tname, __t_desc_type_node);
  1231.       DECL_EXTERNAL (tdecl) = 1;
  1232.       TREE_PUBLIC (tdecl) = 1;
  1233.       tdecl = pushdecl_top_level (tdecl);
  1234.     }
  1235.   /* If we previously defined it, return the defined result.  */
  1236.   else if (definition && DECL_INITIAL (tdecl))
  1237.     return IDENTIFIER_AS_DESC (tname);
  1238.  
  1239.   if (definition)
  1240.     {
  1241.       tree taggr = type;
  1242.       /* Let T* and T& be written only when T is written (if T is an aggr).
  1243.          We do this for const, but not for volatile, since volatile
  1244.      is rare and const is not.  */
  1245.       if (!TYPE_VOLATILE (taggr)
  1246.       && (TREE_CODE (taggr) == POINTER_TYPE
  1247.           || TREE_CODE (taggr) == REFERENCE_TYPE)
  1248.       && IS_AGGR_TYPE (TREE_TYPE (taggr)))
  1249.     taggr = TREE_TYPE (taggr);
  1250.  
  1251.       /* If we know that we don't need to write out this type's
  1252.      vtable, then don't write out it's dossier.  Somebody
  1253.      else will take care of that.  */
  1254.       if (IS_AGGR_TYPE (taggr) && CLASSTYPE_VFIELD (taggr))
  1255.     {
  1256.       if (CLASSTYPE_VTABLE_NEEDS_WRITING (taggr))
  1257.         {
  1258.           TREE_PUBLIC (tdecl) = ! CLASSTYPE_INTERFACE_ONLY (taggr)
  1259.         && CLASSTYPE_INTERFACE_KNOWN (taggr);
  1260.           DECL_EXTERNAL (tdecl) = 0;
  1261.         }
  1262.       else
  1263.         {
  1264.           if (write_virtuals != 0)
  1265.         TREE_PUBLIC (tdecl) = 1;
  1266.         }
  1267.     }
  1268.       else
  1269.     {
  1270.       DECL_EXTERNAL (tdecl) = 0;
  1271.       TREE_PUBLIC (tdecl) = (definition > 1);
  1272.     }
  1273.     }
  1274.   SET_IDENTIFIER_AS_DESC (tname, build_unary_op (ADDR_EXPR, tdecl, 0));
  1275.  
  1276.   if (!definition || DECL_EXTERNAL (tdecl))
  1277.     {
  1278.       /* That's it!  */
  1279.       cp_finish_decl (tdecl, NULL_TREE, NULL_TREE, 0, 0);
  1280.       return IDENTIFIER_AS_DESC (tname);
  1281.     }
  1282.  
  1283.   /* Show that we are defining the t_desc for this type.  */
  1284.   DECL_INITIAL (tdecl) = error_mark_node;
  1285.  
  1286.   parents = build_tree_list (NULL_TREE, integer_zero_node);
  1287.   vbases = build_tree_list (NULL_TREE, integer_zero_node);
  1288.   offsets = build_tree_list (NULL_TREE, integer_zero_node);
  1289.   methods = NULL_TREE;
  1290.   ivars = NULL_TREE;
  1291.  
  1292.   if (TYPE_LANG_SPECIFIC (type))
  1293.     {
  1294.       int i = CLASSTYPE_N_BASECLASSES (type);
  1295.       tree method_vec = CLASSTYPE_METHOD_VEC (type);
  1296.       tree *meth, *end;
  1297.       tree binfos = TYPE_BINFO_BASETYPES (type);
  1298.       tree vb = CLASSTYPE_VBASECLASSES (type);
  1299.  
  1300.       while (--i >= 0)
  1301.     parents = tree_cons (NULL_TREE, build_t_desc (BINFO_TYPE (TREE_VEC_ELT (binfos, i)), 0), parents);
  1302.  
  1303.       while (vb)
  1304.     {
  1305.       vbases = tree_cons (NULL_TREE, build_t_desc (BINFO_TYPE (vb), 0), vbases);
  1306.       offsets = tree_cons (NULL_TREE, BINFO_OFFSET (vb), offsets);
  1307.       vb = TREE_CHAIN (vb);
  1308.     }
  1309.  
  1310.       if (method_vec)
  1311.     for (meth = TREE_VEC_END (method_vec),
  1312.          end = &TREE_VEC_ELT (method_vec, 0); meth-- != end; )
  1313.       if (*meth)
  1314.         {
  1315.           methods = tree_cons (NULL_TREE, build_m_desc (*meth), methods);
  1316.           method_count++;
  1317.         }
  1318.     }
  1319.  
  1320.   if (IS_AGGR_TYPE (type))
  1321.     {
  1322.       for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  1323.     if (TREE_CODE (fields) == FIELD_DECL
  1324.         || TREE_CODE (fields) == VAR_DECL)
  1325.       {
  1326.         ivars = tree_cons (NULL_TREE, build_i_desc (fields), ivars);
  1327.         field_count++;
  1328.       }
  1329.       ivars = nreverse (ivars);
  1330.     }
  1331.  
  1332.   parents = finish_table (NULL_TREE, TYPE_POINTER_TO (__t_desc_type_node), parents, 0);
  1333.   vbases = finish_table (NULL_TREE, TYPE_POINTER_TO (__t_desc_type_node), vbases, 0);
  1334.   offsets = finish_table (NULL_TREE, integer_type_node, offsets, 0);
  1335.   if (methods == NULL_TREE)
  1336.     methods = null_pointer_node;
  1337.   else
  1338.     methods = build_unary_op (ADDR_EXPR,
  1339.                   finish_table (NULL_TREE, __m_desc_type_node, methods, 0),
  1340.                   0);
  1341.   if (ivars == NULL_TREE)
  1342.     ivars = null_pointer_node;
  1343.   else
  1344.     ivars = build_unary_op (ADDR_EXPR,
  1345.                 finish_table (NULL_TREE, __i_desc_type_node, ivars, 0),
  1346.                 0);
  1347.   if (TREE_TYPE (type))
  1348.     target_type = build_t_desc (TREE_TYPE (type), definition);
  1349.   else
  1350.     target_type = integer_zero_node;
  1351.  
  1352.   name_string = combine_strings (build_string (IDENTIFIER_LENGTH (tname)+1, IDENTIFIER_POINTER (tname)));
  1353.  
  1354.   elems = tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, name_string, 0),
  1355.        tree_cons (NULL_TREE,
  1356.               TYPE_SIZE(type)? size_in_bytes(type) : integer_zero_node,
  1357.          /* really should use bitfield initialization here.  */
  1358.          tree_cons (NULL_TREE, integer_zero_node,
  1359.           tree_cons (NULL_TREE, target_type,
  1360.            tree_cons (NULL_TREE, build_int_2 (field_count, 2),
  1361.         tree_cons (NULL_TREE, build_int_2 (method_count, 2),
  1362.          tree_cons (NULL_TREE, ivars,
  1363.           tree_cons (NULL_TREE, methods,
  1364.            tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, parents, 0),
  1365.             tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, vbases, 0),
  1366.              build_tree_list (NULL_TREE, build_unary_op (ADDR_EXPR, offsets, 0))))))))))));
  1367.   return build_generic_desc (tdecl, elems);
  1368. }
  1369.  
  1370. /* Build an initializer for a __i_desc node.  */
  1371. tree
  1372. build_i_desc (decl)
  1373.      tree decl;
  1374. {
  1375.   tree elems, name_string;
  1376.   tree taggr;
  1377.  
  1378.   name_string = DECL_NAME (decl);
  1379.   name_string = combine_strings (build_string (IDENTIFIER_LENGTH (name_string)+1, IDENTIFIER_POINTER (name_string)));
  1380.  
  1381.   /* Now decide whether this ivar should cause it's type to get
  1382.      def'd or ref'd in this file.  If the type we are looking at
  1383.      has a proxy definition, we look at the proxy (i.e., a
  1384.      `foo *' is equivalent to a `foo').  */
  1385.   taggr = TREE_TYPE (decl);
  1386.  
  1387.   if ((TREE_CODE (taggr) == POINTER_TYPE
  1388.        || TREE_CODE (taggr) == REFERENCE_TYPE)
  1389.       && TYPE_VOLATILE (taggr) == 0)
  1390.     taggr = TREE_TYPE (taggr);
  1391.  
  1392.   elems = tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, name_string, 0),
  1393.          tree_cons (NULL_TREE, DECL_FIELD_BITPOS (decl),
  1394.         build_tree_list (NULL_TREE, build_t_desc (TREE_TYPE (decl),
  1395.                               ! IS_AGGR_TYPE (taggr)))));
  1396.   taggr = build (CONSTRUCTOR, __i_desc_type_node, NULL_TREE, elems);
  1397.   TREE_CONSTANT (taggr) = 1;
  1398.   TREE_STATIC (taggr) = 1;
  1399.   TREE_READONLY (taggr) = 1;
  1400.   return taggr;
  1401. }
  1402.  
  1403. /* Build an initializer for a __m_desc node.  */
  1404. tree
  1405. build_m_desc (decl)
  1406.      tree decl;
  1407. {
  1408.   tree taggr, elems, name_string;
  1409.   tree parm_count, req_count, vindex, vcontext;
  1410.   tree parms;
  1411.   int p_count, r_count;
  1412.   tree parm_types = NULL_TREE;
  1413.  
  1414.   for (parms = TYPE_ARG_TYPES (TREE_TYPE (decl)), p_count = 0, r_count = 0;
  1415.        parms != NULL_TREE; parms = TREE_CHAIN (parms), p_count++)
  1416.     {
  1417.       taggr = TREE_VALUE (parms);
  1418.       if ((TREE_CODE (taggr) == POINTER_TYPE
  1419.        || TREE_CODE (taggr) == REFERENCE_TYPE)
  1420.       && TYPE_VOLATILE (taggr) == 0)
  1421.     taggr = TREE_TYPE (taggr);
  1422.  
  1423.       parm_types = tree_cons (NULL_TREE, build_t_desc (TREE_VALUE (parms),
  1424.                                ! IS_AGGR_TYPE (taggr)),
  1425.                   parm_types);
  1426.       if (TREE_PURPOSE (parms) == NULL_TREE)
  1427.     r_count++;
  1428.     }
  1429.  
  1430.   parm_types = finish_table (NULL_TREE, TYPE_POINTER_TO (__t_desc_type_node),
  1431.                  nreverse (parm_types), 0);
  1432.   parm_count = build_int_2 (p_count, 0);
  1433.   req_count = build_int_2 (r_count, 0);
  1434.  
  1435.   if (DECL_VINDEX (decl))
  1436.     vindex = DECL_VINDEX (decl);
  1437.   else
  1438.     vindex = integer_zero_node;
  1439.   if (DECL_CONTEXT (decl)
  1440.       && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (decl))) == 't')
  1441.     vcontext = build_t_desc (DECL_CONTEXT (decl), 0);
  1442.   else
  1443.     vcontext = integer_zero_node;
  1444.   name_string = DECL_NAME (decl);
  1445.   if (name_string == NULL)
  1446.       name_string = DECL_ASSEMBLER_NAME (decl);
  1447.   name_string = combine_strings (build_string (IDENTIFIER_LENGTH (name_string)+1, IDENTIFIER_POINTER (name_string)));
  1448.  
  1449.   /* Now decide whether the return type of this mvar
  1450.      should cause it's type to get def'd or ref'd in this file.
  1451.      If the type we are looking at has a proxy definition,
  1452.      we look at the proxy (i.e., a `foo *' is equivalent to a `foo').  */
  1453.   taggr = TREE_TYPE (TREE_TYPE (decl));
  1454.  
  1455.   if ((TREE_CODE (taggr) == POINTER_TYPE
  1456.        || TREE_CODE (taggr) == REFERENCE_TYPE)
  1457.       && TYPE_VOLATILE (taggr) == 0)
  1458.     taggr = TREE_TYPE (taggr);
  1459.  
  1460.   elems = tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, name_string, 0),
  1461.          tree_cons (NULL_TREE, vindex,
  1462.         tree_cons (NULL_TREE, vcontext,
  1463.            tree_cons (NULL_TREE, build_t_desc (TREE_TYPE (TREE_TYPE (decl)),
  1464.                                ! IS_AGGR_TYPE (taggr)),
  1465.               tree_cons (NULL_TREE, build_c_cast (TYPE_POINTER_TO (default_function_type), build_unary_op (ADDR_EXPR, decl, 0), 0),
  1466.              tree_cons (NULL_TREE, parm_count,
  1467.                 tree_cons (NULL_TREE, req_count,
  1468.                    build_tree_list (NULL_TREE, build_unary_op (ADDR_EXPR, parm_types, 0)))))))));
  1469.  
  1470.   taggr = build (CONSTRUCTOR, __m_desc_type_node, NULL_TREE, elems);
  1471.   TREE_CONSTANT (taggr) = 1;
  1472.   TREE_STATIC (taggr) = 1;
  1473.   TREE_READONLY (taggr) = 1;
  1474.   return taggr;
  1475. }
  1476. #endif /* dossier */
  1477.  
  1478.  
  1479. /* Conditionally emit code to set up an unwind-protect for the
  1480.    garbage collector.  If this function doesn't do anything that involves
  1481.    the garbage collector, then do nothing.  Otherwise, call __gc_push
  1482.    at the beginning and __gc_pop at the end.
  1483.  
  1484.    NOTE!  The __gc_pop function must operate transparently, since
  1485.    it comes where the logical return label lies.  This means that
  1486.    at runtime *it* must preserve any return value registers.  */
  1487.  
  1488. void
  1489. expand_gc_prologue_and_epilogue ()
  1490. {
  1491.   extern tree maybe_gc_cleanup;
  1492.   struct rtx_def *last_parm_insn, *mark;
  1493.   extern struct rtx_def *get_last_insn ();
  1494.   extern struct rtx_def *get_first_nonparm_insn ();
  1495.   extern struct rtx_def *previous_insn ();
  1496.   tree action;
  1497.  
  1498.   /* If we didn't need the obstack, don't cons any space.  */
  1499.   if (current_function_obstack_index == 0
  1500.       || current_function_obstack_usage == 0)
  1501.     return;
  1502.  
  1503.   mark = get_last_insn ();
  1504.   last_parm_insn = get_first_nonparm_insn ();
  1505.   if (last_parm_insn == 0) last_parm_insn = mark;
  1506.   else last_parm_insn = previous_insn (last_parm_insn);
  1507.  
  1508.   action = build_function_call (gc_push_fndecl,
  1509.                 build_tree_list (NULL_TREE, size_int (++current_function_obstack_index)));
  1510.   expand_expr_stmt (action);
  1511.  
  1512.   reorder_insns (next_insn (mark), get_last_insn (), last_parm_insn);
  1513.  
  1514.   /* This will be expanded as a cleanup.  */
  1515.   TREE_VALUE (maybe_gc_cleanup)
  1516.     = build_function_call (gc_pop_fndecl, NULL_TREE);
  1517. }
  1518.  
  1519. /* Some day we'll use this function as a call-back and clean
  1520.    up all the unnecessary gc dribble that we otherwise create.  */
  1521. void
  1522. lang_expand_end_bindings (first, last)
  1523.      struct rtx_def *first, *last;
  1524. {
  1525. }
  1526.  
  1527. void
  1528. init_gc_processing ()
  1529. {
  1530.   tree parmtypes = hash_tree_chain (class_star_type_node,
  1531.                     hash_tree_chain (integer_type_node, NULL_TREE));
  1532.   gc_protect_fndecl = define_function ("__gc_protect",
  1533.                        build_function_type (class_star_type_node, parmtypes),
  1534.                        NOT_BUILT_IN, 0, 0);
  1535.  
  1536.   parmtypes = hash_tree_chain (integer_type_node, NULL_TREE);
  1537.   gc_unprotect_fndecl = define_function ("__gc_unprotect",
  1538.                      build_function_type (void_type_node, parmtypes),
  1539.                      NOT_BUILT_IN, 0, 0);
  1540.  
  1541.   gc_push_fndecl = define_function ("__gc_push",
  1542.                     TREE_TYPE (gc_unprotect_fndecl),
  1543.                     NOT_BUILT_IN, 0, 0);
  1544.  
  1545.   gc_pop_fndecl = define_function ("__gc_pop",
  1546.                    build_function_type (void_type_node,
  1547.                             void_list_node),
  1548.                    NOT_BUILT_IN, 0, 0);
  1549.   gc_nonobject = build_int_2 (0x80000000, 0);
  1550.   gc_visible = build_int_2 (0x40000000, 0);
  1551.   gc_white = integer_zero_node;
  1552.   gc_offwhite = build_int_2 (0x10000000, 0);
  1553.   gc_grey = build_int_2 (0x20000000, 0);
  1554.   gc_black = build_int_2 (0x30000000, 0);
  1555. }
  1556.